// utility graphs

tempfile temp

use "${Data_Clean}/spatial_RD_regdata.dta", clear

local delta_demog "delta_population delta_pop_density delta_income"
local delta_dist "cons i.groupid#c.IOU_dist i.groupid#c.muni_dist"
local other_cov "delta_fuel_economy_mean delta_hybrid_share delta_luxury_share delta_mud_hh_share"
local price_var "delta_p_e_active delta_obswgtprice_lt3_"

*Retain observations used for column 3 main table
reghdfe delta_share_BEV `price_var' `delta_demog' `other_cov', absorb(`delta_dist') vce(cluster cbg0 cbg1) resid
gen insample = e(sample)

keep if insample==1

gen IOU_dum1 = adj_IOU
gen IOU_dum0 = ref_IOU

keep IOU_dum* utility_name* population* pop_density* income* hybrid_share* luxury_share* ///
mud_hh_share* fuel_economy_mean* p_e_high* p_e_active* P_gas* obswgtprice_lt3_* share_BEV* cbg* IOU_dist muni_dist year

reshape long IOU_dum utility_name population pop_density income hybrid_share luxury_share ///
mud_hh_share fuel_economy_mean p_e_high p_e_active P_gas obswgtprice_lt3_ share_BEV, i(cbg0 cbg1 IOU_dist muni_dist year) j(old_adj)

gen double cbg = cbg0 if old_adj == 0
replace cbg = cbg1 if old_adj == 1

*Note: distances IOU_dist / muni_dist are constructed to be relative moving from the IOU to non-IOU side of the border

gen dist_to_border = abs(IOU_dist) if IOU_dum==1
replace dist_to_border = abs(muni_dist) if IOU_dum==0

drop cbg0 cbg1 IOU_dist muni_dist old_adj

collapse (mean) population pop_density income hybrid_share luxury_share ///
mud_hh_share fuel_economy_mean p_e_high p_e_active P_gas obswgtprice_lt3_ share_BEV dist_to_border, by(cbg IOU_dum utility_name year)

gen dataset = "RD Subsample"

/*Scale to match scale in Panel sample*/
foreach x in income population pop_density dist_to_border {
	replace `x' = `x'/1000
}

foreach x in P_gas p_e_active obswgtprice_lt3_ {
	replace `x' = `x'*100
}

rename obswgtprice_lt3_ obswgtprice_lt3

gen dum_PGE = (utility_name == "Pacific Gas & Electric Company")
gen dum_SCE = (utility_name == "Southern California Edison")
gen dum_SDGE = (utility_name == "San Diego Gas & Electric")

save `temp', replace

use "${Data_Clean}/panel_regdata.dta", clear


gen IOU_dum = dum_SCE + dum_SDGE + dum_PGE
gen dataset = "Full Sample"


collapse (mean) p_e_active obswgtprice_lt3 population pop_density income hybrid_share luxury_share mud_hh_share fuel_economy_mean dum_PGE dum_SCE dum_SDGE (sum) share_BEV, by(cbg year dataset IOU_dum)


append using `temp'


gen IOU = "IOU" if IOU_dum==1
replace IOU = "Muni" if IOU_dum==0

gen dataset_IOU = dataset + IOU


label var share_BEV "Annual EV Sales per 10000 pop"
label var p_e_active "Marg. Elec. Price (cpkwh)"
label var population "Population (000s)"
label var pop_density "Population Density"
label var income "Household Income (000s)"
label var obswgtprice_lt3 "Gasoline Price (cpg)"
label var dist_to_border "Dist. to Util. Boundary (km)"
label var dum_SCE "Fraction in SCE"
label var dum_SDGE "Fraction in SDGE"
label var dum_PGE "Fraction in PGE"
label var hybrid_share "Hybrid Fleet Share (2013)"
label var luxury_share "Luxury Fleet Share (2013)"
label var mud_hh_share "MUD HH share (2013)"
label var fuel_economy_mean "Fleet Fuel Economy (mpg, 2013)"


local sumstats_fullsample "share_BEV p_e_active obswgtprice_lt3 population pop_density income hybrid_share luxury_share mud_hh_share fuel_economy_mean dum_PGE dum_SCE dum_SDGE"

local sumstats_RDsample "share_BEV p_e_active obswgtprice_lt3 population pop_density income hybrid_share luxury_share mud_hh_share fuel_economy_mean dum_PGE dum_SCE dum_SDGE dist_to_border"

estpost sum `sumstats_fullsample' if dataset=="Full Sample" & IOU_dum==1
eststo fs_IOU

estpost sum `sumstats_fullsample' if dataset=="Full Sample" & IOU_dum==0
eststo fs_Muni

estpost ttest `sumstats_fullsample' if dataset=="Full Sample", by(IOU) unequal
eststo fs_diff

estpost sum `sumstats_RDsample' if dataset=="RD Subsample" & IOU_dum==1
eststo rd_IOU

estpost sum `sumstats_RDsample' if dataset=="RD Subsample" & IOU_dum==0
eststo rd_Muni

estpost ttest `sumstats_RDsample' if dataset=="RD Subsample", by(IOU) unequal
eststo rd_diff

estout fs_IOU fs_Muni fs_diff rd_IOU rd_Muni rd_diff using "${ResultsOut}/Tables/sum_stats_table_wttests.tex",  cells("mean(fmt(3) pattern(1 1 0 1 1 0)) b(fmt(3) star pattern(0 0 1 0 0 1))" "sd(fmt(3) par pattern(1 1 0 1 1 0)) se(fmt(3) par pattern(0 0 1 0 0 1))") label ///
mlabels("Full Sample" "RD Subsample", span prefix(\multicolumn{@span}{c}{) suffix(})) ///
nonum replace

 
estpost tabstat `sumstats_fullsample' if dataset=="Full Sample", by(IOU) statistics(mean sd) columns(statistics) nototal
eststo m1

estpost tabstat `sumstats_RDsample' if dataset=="RD Subsample", by(IOU) statistics(mean sd) columns(statistics) nototal
eststo m2

esttab m1 m2 using "${ResultsOut}/Tables/sum_stats_table.tex" , main(mean) aux(sd) nostar unstack nonote label ///
mlabels("Full Sample" "RD Subsample", span prefix(\multicolumn{@span}{c}{) suffix(})) ///
nomtitles nonum replace


